package com.manli.api.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.manli.api.pojo.QueryParamsUserPass;
import com.manli.api.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import com.manli.api.base.annotation.Permissions;
import com.manli.api.dto.User;
import com.manli.api.service.UserService;



/**
 * 
 *
 * @author manli
 * @email 
 * @date 2020-04-27 19:40:52
 */
@Api(value="/user", tags="")
@RestController
@RequestMapping("api/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @ApiOperation(value = "列表",notes = "")
    @PostMapping("/list")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN_PERMISSONS)
    public Result list(@RequestBody Map<String, Object> params){
        return userService.queryPage(params);
    }

    /**
     * 全部数据信息
     */
    @ApiOperation(value = "全部数据信息",notes = "")
    @GetMapping("/all")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN_PERMISSONS)
    public Result all(){
        return userService.queryAll();
    }

    /**
     * 单条信息
     */
    @ApiOperation(value = "单条信息",notes = "单条信息")
    @GetMapping("/info/{userId}")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN_PERMISSONS)
    public Result info(@PathVariable("userId") Integer userId){
        return userService.getById(userId);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存",notes = "保存")
    @PostMapping("/save")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN_PERMISSONS)
    public Result save(@RequestBody User user,HttpServletRequest request){
        return userService.save(user,request);
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改",notes = "修改")
    @PostMapping("/update")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN_PERMISSONS)
    public Result update(@RequestBody User user){
        return userService.updateById(user);
    }

    /**
     * 忘记密码修改
     */
    @ApiOperation(value = "密码修改",notes = "密码修改")
    @PostMapping("/passwordUpdate")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_OPEN)
    public Result passwordUpdate(@RequestBody User user)throws Exception{
        return userService.passwordUpdate(user);
    }

    /**
     * 已登录密码修改
     */
    @ApiOperation(value = "密码修改",notes = "密码修改")
    @PostMapping("/passwordUpdateLogin")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result passwordUpdateLogin(@RequestBody QueryParamsUserPass user,HttpServletRequest request)throws Exception{
        return userService.passwordUpdateLogin(user,request);
    }

    /**
     * 删除单条数据
     */
    @ApiOperation(value = "删除",notes = "删除")
    @ApiImplicitParam(name = "userIds", value = "id",dataType = "int", required = true)
    @DeleteMapping("/delete")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN_PERMISSONS)
    public Result delete( Integer userId){
        return userService.removeById(userId);
    }

    /**
     * 删除多条数据
     */
    @ApiOperation(value = "删除",notes = "删除")
    @ApiImplicitParam(name = "userIds", value = "id数组,json字符串",dataType = "id数组,json字符串", required = true)
    @DeleteMapping("/deleteIds")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN_PERMISSONS)
    public Result deleteIds(@RequestBody Integer[] userIds){
        return userService.removeByIds(Arrays.asList(userIds));
    }

}
